var app = getApp();
Component({
    properties: {
        info: Object,
        isPush : Boolean,
        subscribe : Boolean,
        params : Object,
    },
    data: {
        top : 25,
        isshow : true,
        member : 1,
        type : 0,
        isshowdy : false,
    },
    options : {
        addGlobalClass : true,
    },
    ready: function() {
        var self = this;
        var n = wx.getMenuButtonBoundingClientRect();
        this.setData({
            top : n.top,
        })
    },
    methods: {
        back : function( e ){
            app.com.backPage(0);
        },
        onShow(){
            this.setData({
                isshow : !this.data.isshow
            })
        },
        changeNum(e){
            this.setData({
                member : e && e.num > 0 ? e.num : 1,
            })
        },
        onsub(){

            var self = this;
            if(self.data.params.xcxmess && wx.requestSubscribeMessage){
                wx.requestSubscribeMessage({
                    tmplIds : self.data.params.xcxmess,
                    complete : function(){
                        self.sub();
                    }
                })
            }else{
                self.sub();
            }
        },
        sub(){
            var self = this;
            self.data.type = 1;
            if(self.data.info.isauth == 1 && self.data.isauth){
                self.loginauth = this.selectComponent('#loginauth');
                self.loginauth.hideauth();
                return false;
            }

            var postdata = {
                op : 'sublive',
                liveid :  self.data.info.id,
            };
            
            app.com.http('live','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    self.setData({
                        'info.issub' : self.data.info.issub*1 == 1 ? 0 : 1,
                        isshowdy : self.data.info.isdy*1 == 1 ? false : true,
                    })
                    self.triggerEvent('dy',{type:'issub',value:1});
                    if( res.data.data.mess ){
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                    }
                    
                    app.com.toast(res.data.message);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        },
        dyfn(){
            var self = this;
            if(self.data.params.xcxmess && wx.requestSubscribeMessage){
                wx.requestSubscribeMessage({
                    tmplIds : self.data.params.xcxmess,
                    complete : function(){
                        self.dy();
                    }
                })
            }else{
                self.dy();
            }
        },
        dy(){
            var self = this;
            self.data.type = 2;
            if(self.data.info.isauth == 1 && self.data.isauth){
                self.loginauth = this.selectComponent('#loginauth');
                self.loginauth.hideauth();
                return false;
            }

            var postdata = {
                op : 'dylive',
                liveid :  self.data.info.id,
            };
            
            app.com.http('live','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    self.setData({
                        'info.isdy' : self.data.info.isdy*1 == 1 ? 0 : 1,
                        isshowdy : false,
                    })
                    self.triggerEvent('dy',{type:'isdy',value:1});
                    if( res.data.data.mess ){
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                    }
                    app.com.toast(res.data.message);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        },
        onClosedy(){
            this.setData({
                isshowdy : false,
            })
        },
        authed(){
            var self = this;
            self.setData({
                isauth : false,
            })
            if(self.data.type == 1){
                self.sub();
            }else{
                self.dy();
            }
        }
    }

})